package br.com.solutions.delivery.controller;

import br.com.solutions.delivery.entity.Route;
import br.com.solutions.delivery.intf.Travel;
import br.com.solutions.delivery.screen.RouteScreen;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RouteScreenController {
    
    private Travel travel;
    
    private Route route = new Route();
    private RouteScreen form;
    
    private static RouteScreenController instance;
    
    private class AdicionarRotaActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            form.setVisible(false);
            travel.addRoute(route);
        }

    }

    private class LocalFinalActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            LocationScreenController.getInstance(route.getTo()).showScreen();
        }
    }

    private class LocalPartidaActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            LocationScreenController.getInstance(route.getFrom()).showScreen();            
        }
    }
    private RouteScreenController(){
        this.form = RouteScreen.getInstance();
        this.form.getBtnAdicionarRota().addActionListener(new AdicionarRotaActionListener());
        this.form.getBtnLocalFinal().addActionListener(new LocalFinalActionListener());
        this.form.getBtnLocalPartida().addActionListener(new LocalPartidaActionListener());
    }
    
    public static RouteScreenController getInstance(Travel travel){
        if (instance == null)
            instance = new RouteScreenController();
        instance.travel = travel;
        return instance;
    }
    
    public void showScreen(){
        this.form.setVisible(true);
    }
    
}
